package com.lemon.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lemon.domain.PageResult;
import com.lemon.domain.Warehouse;
import com.lemon.domain.WarehouseArea;
import com.lemon.domain.WarehouseAreaLoca;
import com.lemon.domain.vo.WarehouseAreaLocaVo;
import com.lemon.mapper.WarehouseAreaLocaMapper;
import com.lemon.mapper.WarehouseAreaMapper;
import com.lemon.mapper.WarehouseMapper;
import com.lemon.service.WarehouseAreaLocaService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class WarehouseAreaLocaServiceImpl extends ServiceImpl<WarehouseAreaLocaMapper, WarehouseAreaLoca> implements WarehouseAreaLocaService {

    private final WarehouseAreaLocaMapper warehouseAreaLocaMapper;

    private final WarehouseMapper warehouseMapper;

    private final WarehouseAreaMapper warehouseAreaMapper;

    @Override
    public PageResult getPage(Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<WarehouseAreaLoca> warehouseAreaLocas = warehouseAreaLocaMapper.selectList(null);
        Page pageInfo = (Page) warehouseAreaLocas;
        return new PageResult(pageInfo.getTotal(),pageInfo.getResult());
    }

    @Override
    public void add(WarehouseAreaLoca warehouseAreaLoca) {
        extracted(warehouseAreaLoca);
        warehouseAreaLoca.setCreateTime(LocalDateTime.now());
        warehouseAreaLocaMapper.insert(warehouseAreaLoca);
    }

    private void extracted(WarehouseAreaLoca warehouseAreaLoca) {
        Warehouse warehouse = warehouseMapper.selectById(warehouseAreaLoca.getWarehouseId());
        warehouseAreaLoca.setWarehouseName(warehouse.getName());
        WarehouseArea warehouseArea = warehouseAreaMapper.selectById(warehouseAreaLoca.getWarehouseAreaId());
        warehouseAreaLoca.setWarehouseAreaName(warehouseArea.getName());
    }

    @Override
    public void updateWarehouseLoca(WarehouseAreaLoca warehouseAreaLoca) {
        extracted(warehouseAreaLoca);
        warehouseAreaLoca.setUpdateTime(LocalDateTime.now());
        warehouseAreaLocaMapper.updateById(warehouseAreaLoca);
    }

    @Override
    public List<WarehouseAreaLocaVo> getWarehouseLocaList(Integer warehouseAreaId) {
        return warehouseAreaLocaMapper.getWarehouseAreaList(warehouseAreaId);
    }
}
